package zhtt;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.sql.JoinType;
import org.junit.Test;

import java.util.List;

/**
 * Created by zhtt on 2017/4/12.
 */
public class JunitTest01 extends JunitTestUtil {

    @Test
    /**
     * 左外连接
     */
    public void query(){
        Session session=HbnUtil.getSession();
        try{
            session.beginTransaction();
            String hql=" from Country";
            List<Country> list=session.createQuery(hql).list();
            for(Country country:list){
                System.out.println(country);
                System.out.println("---------------------------------------------\n");
            }
            session.getTransaction().commit();
        }catch (Exception e){e.printStackTrace();}
    }

    @Test
    /**
     * 左外连接
     */
    public void left1(){
        Session session=HbnUtil.getSession();
        try{
            session.beginTransaction();
            String hql=" from Country c left outer join c.ministerSet";
            List<Object[]> list=session.createQuery(hql).list();
            for(Object[] obj:list){
                System.out.println(obj[0]);
                System.out.println(obj[1]);
                System.out.println("---------------------------------------------");
            }
            session.getTransaction().commit();
        }catch (Exception e){e.printStackTrace();}
    }

    @Test
    /**
     * 迫切左外连接
     */
    public void leftFetch1(){
        Session session=HbnUtil.getSession();
        try{
            session.beginTransaction();
            String hql=" from Country c left outer join fetch c.ministerSet";
            List<Country> list=session.createQuery(hql).list();
            for(Country country:list){
                System.out.println(country);
                System.out.println("---------------------------------------------\n");
            }
            session.getTransaction().commit();
        }catch (Exception e){e.printStackTrace();}
    }

    @Test
    /**
     * 迫切左外连接去重
     */
    public void leftFetch2(){
        Session session=HbnUtil.getSession();
        try{
            session.beginTransaction();
            String hql="select distinct c from Country c left outer join fetch c.ministerSet";
            List<Country> list=session.createQuery(hql).list();
            for(Country country:list){
                System.out.println(country);
                System.out.println("---------------------------------------------\n");
            }
            session.getTransaction().commit();
        }catch (Exception e){e.printStackTrace();}
    }

    @Test
    /**
     * 左外连接QBC实现（去重）
     */
    public void leftQbc(){
        Session session=HbnUtil.getSession();
        try {
            session.beginTransaction();
            List<Country> countries=session.createCriteria(Country.class)
                    .createCriteria("ministerSet", JoinType.LEFT_OUTER_JOIN)
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                    .list();
            for(Country country:countries){
                System.out.println(country);
                System.out.println("---------------------------------------------\n");
            }
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    /**
     * 命名查询
     */
    public void namedQuery(){
        Session session=HbnUtil.getSession();
        try {
            session.beginTransaction();
            List<Country> countries=session.getNamedQuery("queryAll").list();
            for(Country country:countries){
                System.out.println(country);
                System.out.println("---------------------------------------------\n");
            }
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
